package com.sq.universe.flowable.listener;

import com.sq.universe.base.entity.BusinessException;
import com.sq.universe.base.entity.RespEntity;
import com.sq.universe.flowable.service.WorkflowService;
import org.flowable.common.engine.impl.el.FixedValue;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.task.service.delegate.DelegateTask;
import org.flowable.task.service.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Component(value = "commonTaskListener")
public class CommonTaskListener  implements TaskListener, Serializable {


    @Autowired
    RestTemplate restTemplate;

    @Autowired
    @Lazy
    WorkflowService workflowService;

    private FixedValue module;
    private FixedValue url;

    /**
     * 监听器中无法获取spring bean配置
     */

    @Override
    public void notify(DelegateTask delegateTask) {

        Map taskInfo = workflowService.queryTaskInfo(delegateTask.getProcessInstanceId());
        Map map = new HashMap<>();
        map.put("processInstanceId",delegateTask.getProcessInstanceId());
        map.putAll(taskInfo);
        RespEntity respEntity = (RespEntity)restTemplate.postForObject("http://" + module.getExpressionText() + url.getExpressionText(),map,Map.class);
        if(respEntity.getCode() == 500){
            throw new BusinessException("500",respEntity.getMessage());
        }
    }



}
